<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class UserController extends AppUserBaseC {

    protected $cacheTime = 300;

    //获取用户资料
    public function actionInfo() {
        if (empty($this->uid)) {
            $this->ajaxReturn(array('info' => '获取用户UID失败！', 'error_code' => 1101));
        }
        $data = array(
            'uid' => $this->userInfo['uid'],
            'username' => $this->userInfo['username'],
            'group_id' => $this->userInfo['group_id'],
            'type' => $this->userInfo['type'],
            'email' => $this->userInfo['email'],
            'status' => $this->userInfo['status'],
            'regip' => $this->userInfo['regip'],
            'regdate' => $this->userInfo['regdate'],
            'lastloginip' => $this->userInfo['lastloginip'],
            'lastlogintime' => $this->userInfo['lastlogintime'],
            'score' => $this->userInfo['score'],
            'sex' => $this->userInfo['sex'],
            'year' => $this->userInfo['year'],
            'month' => $this->userInfo['month'],
            'day' => $this->userInfo['day'],
        );
        if (!empty($data)) {
            $this->ajaxReturn(array('status' => true, 'data' => $data));
        } else {
            $this->ajaxReturn(array('status' => false, 'data' => '', 'error_code' => 1102));
        }
    }

    //可修改性别生日App

    public function actionMod() {
        $modelObj = MembersModel::model()->findByPk($this->uid);
        if (empty($modelObj)) {
            $modelObj = new MembersModel();
        }
        $data['sex'] = Yii::app()->request->getParam('sex');
        if (empty($data['sex'])) {
            $this->ajaxReturn(array('status' => false, 'info' => "请填写性别!"));
        }
        $data['year'] = Yii::app()->request->getParam('year');
        if (empty($data['year'])) {
            $this->ajaxReturn(array('status' => false, 'info' => "请填写年!"));
        }
        $data['month'] = Yii::app()->request->getParam('month');
        if (empty($data['month'])) {
            $this->ajaxReturn(array('status' => false, 'info' => "请填写性月"));
        }
        $data['day'] = Yii::app()->request->getParam('day');
        if (empty($data['day'])) {
            $this->ajaxReturn(array('status' => false, 'info' => "请填写日!"));
        }
        $modelObj->sex = $data['sex'];
        $modelObj->year = $data['year'];
        $modelObj->month = $data['month'];
        $modelObj->day = $data['day'];
        if ($modelObj->validate() && $modelObj->save()) {
            $this->getUserInfo(false);
            $this->ajaxReturn(array('status' => true, 'info' => "修改成功!"));
        }
    }

    //积分记录
    public function actionInteglog() {
        $offset = Yii::app()->request->getParam('offset');
        $num = Yii::app()->request->getParam('num');
        if (empty($num)) {
            $num = 20;
        }
        $model = MembersActionModel::model();
        $criteria = $model->where(array('uid' => $this->uid, 'order' => 'action_time desc'));
        $count = (int) $model->count($criteria);
        //之前版本分页 +1
        if (((int) $this->version <= 11 && $this->platform == 'Android') || ((float) $this->version < 1.3 && $this->platform == 'iOS')) {
            $offset = $offset + 1;
        }
        $page = self::page($count, $num, $offset);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $scores = $model->findAll($criteria);
        $dataList = array();
        foreach ($scores as $t) {
            $dataList[] = $t->attributes;
        }
        if (!empty($dataList)) {
            $this->ajaxReturn(array('status' => true, 'data' => $dataList, 'count' => $count, 'limit' => $num, 'score' => $this->userInfo['score']));
        } else {
            $this->ajaxReturn(array('status' => false, 'data' => '', 'count' => 0, 'limit' => '', 'score' => '', 'error_code' => 1103));
        }
    }

    //手机签到
    public function actionSign() {
        if (empty($this->uid)) {
            $this->ajaxReturn(array('info' => '获取用户UID失败！', 'error_code' => 1101));
        }
        $this->getUserInfo(false);
        $model = new MembersSignModel();
        $score = $model->signIn($this->uid, 1);
        if ($score == false) {
            $this->ajaxReturn(array('status' => 0, 'info' => $model->getOneError() ? $model->getOneError() : '对不起,签到失败！', 'error_code' => 1015));
        } else {
            //双十二挖宝箱钥匙获取
            $chest = S12ChanceModel::model()->run($this->userInfo, 1);
            //判断网站版是否有签到
            if ($model->isSignIn($this->uid, 1) === FALSE) {
                $this->ajaxReturn(array('status' => 1, 'info' => '恭喜您，签到获得' . $score . '个积分！您还可以前往网站端签到将再获5个积分！', 'error_code' => 10000));
            } else {
                $this->ajaxReturn(array('status' => 1, 'info' => '恭喜您，签到获得' . $score . '个积分！明天再签再送哦！', 'error_code' => 10000));
            }
        }
    }

    /**
     * 不付费报名信息查询
     */
    public function actionFreeBm() {
        $model = new GoodsBmModel();
        $offset = (int) $_GET['offset'];
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 10;
        }
        $criteria = $model->where(array('uid' => $this->uid, 'pay_status' => "0"));
        $criteria->limit = $num;
        $criteria->offset = $offset;
        $data = $model->findAll($criteria);
        $count = (int) count($data);
        $dataList = $this->Bmlist($data);
        if (!empty($dataList)) {
            $this->ajaxReturn(array('status' => true, 'count' => $count, 'data' => $dataList,));
        }
        $this->ajaxReturn(array('status' => false, 'count' => '', 'data' => '', 'error_code' => 1104));
    }

    /**
     * 我的广告位
     */
    public function actionMyPress() {
        $MoneyOrderModel = new MoneyPositionOrderModel();
        $offset = (int) $_GET['offset'];
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 10;
        }
        $criteria = $MoneyOrderModel->where(array('uid' => $this->uid,));
        $criteria->limit = $num;
        $criteria->offset = $offset;
        $ads = $MoneyOrderModel->findAll($criteria);
        $data = array();
        if (!empty($ads)) {
            foreach ($ads as $d => $ad) {
                $data[$d]['order_sn'] = $ad->order_sn;
                $data[$d]['order_title'] = $ad->order_title;
                $data[$d]['add_time'] = $ad->add_time;
                $data[$d]['money'] = $ad->money;
                $data[$d]['paystatus'] = $ad->status;
                if ($ad->status == 0) {
                    $data[$d]['paystatus'] = '未付款，不可用';
                } elseif ($ad->status == 1) {
                    $data[$d]['paystatus'] = '已付款，可使用';
                } elseif ($ad->status == 2) {
                    $data[$d]['paystatus'] = '已付款，已使用';
                } else {
                    $data[$d]['paystatus'] = '未知';
                }
            }
            $this->ajaxReturn(array(status => 1, 'data' => $data));
        }
        $this->ajaxReturn(array(status => 0, 'data' => '', 'error_code' => 1104));
    }

    /**
     * 付费报名信息查询
     */
    public function actionPayBm() {
        $model = new GoodsBmModel();
        $offset = (int) $_GET['offset'];
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 10;
        }
        $criteria = $model->where(array('uid' => $this->uid, 'pay_status' => array('NEQ', 0)));
        $criteria->limit = $num;
        $criteria->offset = $offset;
        $data = $model->findAll($criteria);
        $count = (int) count($data);
        $dataList = $this->Bmlist($data);
        if (!empty($dataList)) {
            $this->ajaxReturn(array('status' => true, 'count' => $count, 'data' => $dataList,));
        }
        $this->ajaxReturn(array('status' => false, 'count' => '', 'data' => '', 'error_code' => 1104));
    }

    /**
     * 数组整理
     * @param type $data 输入数据
     * @return boolean
     */
    static function Bmlist($data) {
        if (empty($data)) {
            return false;
        }
        $model = new CategoryModel();
        $dataList = array();
        foreach ($data as $o => $bm) {
            $dataList[$o]['product_id'] = $bm->product_id;
            $dataList[$o]['goods_id'] = $bm->goods_id;
            $dataList[$o]['uid'] = $bm->uid;
            $dataList[$o]['goods_name'] = $bm->goods_name;
            $dataList[$o]['add_time'] = $bm->add_time;
            $dataList[$o]['start_time'] = $bm->start_time;
            $dataList[$o]['end_time'] = $bm->end_time;
            $dataList[$o]['detail_url'] = $bm->detail_url;
            $dataList[$o]['product_img'] = $bm->product_img;
            $dataList[$o]['promo_price'] = $bm->promo_price;
            $dataList[$o]['p_price'] = $bm->p_price;
            $dataList[$o]['cat_id'] = $bm->cat_id;
            $dataList[$o]['cat_name'] = $model->getCategory($bm->cat_id, 'cat_name', true);
            $dataList[$o]['isdelivery'] = $bm->isdelivery;
            $dataList[$o]['status'] = $bm->status;
            $dataList[$o]['admin_id'] = $bm->admin_id;
            if ($bm->status == 0 && ($bm->admin_id != 0)) {
                $dataList[$o]['status'] = 5;
            }
            $dataList[$o]['note'] = $bm->note;
            $dataList[$o]['pay_status'] = $bm->pay_status;
        }
        return $dataList;
    }

    public function actionPutpid() {
        $goodsBm = GoodsBmModel::model();
        $data = $goodsBm->findAll($goodsBm->where(array('uid' => $this->uid, 'order' => 'goods_id desc')));
        $return = array();
        foreach ($data as $bm) {
            $return[]['product_id'] = $bm->product_id;
        }
        $this->ajaxReturn(array('status' => true, 'data' => $return));
    }

    /**
     * 报名进度查询
     */
    public function actionLookUp() {
        $product_id = Yii::app()->request->getParam('product_id');
        if (empty($product_id)) {
            $this->ajaxReturn(array('status' => 0, 'info' => '请输入商品ID！', 'error_code' => 1012));
            exit;
        }
        if (!is_numeric($product_id)) {
            exit;
        }
//        $cacheKey = "userCx_actionLookUp_{$product_id}_" . $this->uid;       
//        if (empty($cache)) { 
//           $cache = Yii::app()->cache->get($cacheKey);
//        }
        $goodsAction = GoodsActionModel::model();
        $goodsBm = GoodsBmModel::model();
        $model = new CategoryModel();
        $data = array();
        $time = time();
        //查询到的最新商品信息
        $goodsInfo = $goodsBm->find($goodsBm->where(array('product_id' => $product_id, 'order' => 'goods_id desc')));
        if (!empty($goodsInfo)) {
            $data['goods_name'] = $goodsInfo->goods_name;
            $data['detail_url'] = $goodsInfo->detail_url;
            $data['product_img'] = $goodsInfo->product_img;
            $data['promo_price'] = $goodsInfo->promo_price;
            $data['cat_id'] = $goodsInfo->cat_id;
            $data['cat_name'] = $model->getCategory($goodsInfo->cat_id, 'cat_name', true);
            $data['stock'] = $goodsInfo->stock;
            $data['isdelivery'] = $goodsInfo->isdelivery;
            $data['note'] = isset($goodsInfo->note) ? $goodsInfo->note : '';
            if ($goodsInfo->start_time < $time && $goodsInfo->end_time > $time && $goodsInfo->status == 1) {
                $data['note'] = '恭喜您，您的商品目前正在展示。<br />上架时'
                        . '间为：' . date('m-d H:i', $goodsInfo->start_time) . ' 至 ' . date('m-d H:i', $goodsInfo->end_time);
            } elseif ($goodsInfo->start_time > $time && $goodsInfo->status == 1) {
                $data['note'] = '恭喜您，商品已经通过审核，目前属于预告状态。<br />上架时间为：' . date('m-d H:i', $goodsInfo->start_time) . ' 至 ' . date('m-d H:i', $goodsInfo->end_time);
            } elseif ($goodsInfo->status == 2) {
                //$adminInfo = AdminUser::model()->getUserInfo((int) $goodsInfo->admin_id);
                $data['note'] = '商品当前状态 审核不通过！<br/>不通过原因，请看网页下面审核流程。';
            } elseif ($goodsInfo->status == 3) {
                $adminInfo = AdminUser::model()->getUserInfo((int) $goodsInfo->admin_id);
                $data['note'] = '商品当前状态 邮寄样品中等待审核！<br/>寄样品质检（看到请联系招商客服旺旺-<a href="http://amos.im.alisoft.com/msg.aw?v=2&amp;site=cntaobao&amp;s=2&amp;charset=utf-8&amp;uid=' . $adminInfo['wangwang'] . '" target="_blank">' . $adminInfo['wangwang'] . '<img src="http://amos.im.alisoft.com/online.aw?v=2&amp;site=cntaobao&amp;s=2&amp;charset=utf-8&amp;uid=' . $adminInfo['wangwang'] . '" style="height:16px; width:16px"></a>) <br/>地址：福建省 厦门市 集美区 杏林湾商务运营中心2号楼24层1折网公司<br/> （最好发顺丰，节省时间）(' . $adminInfo['wangwang'] . ' 收) 180-3015-1020 <br/>样品无寄回，请郑重哦（样品里面记得附上产品id）不收到付的样品。';
            } elseif ($goodsInfo->status == 0) {
                $data['note'] = '该商品正在审核中,请您耐心等待！';
            } elseif ($goodsInfo->status == 1 && $goodsInfo->end_time < $time) {
                $data['note'] = '该商品已经下架，上次上架时间为：' . date('m-d H:i', $goodsInfo->start_time) . ' 至 ' . date('m-d H:i', $goodsInfo->end_time);
            } else {
                $data['note'] = '对不起,商品没能正常显示！';
            }
        } else {
            $TaoapiItem = TaobaoApi::getInstance()->getItem($product_id);
            if (isset($TaoapiItem['title'])) {
                $data['goods_name'] = $TaoapiItem['title'];
                $data['detail_url'] = $TaoapiItem['detail_url'];
                $data['product_id'] = $product_id;
                $data['product_img'] = $TaoapiItem['pic_url'];
                $data['note'] = '目前商品未正常显示，具体详情如下：';
            } else {
                $data['product_id'] = 0;
            }
        }
        //最新商品进度
        $firstactionList = $goodsAction->findAll($goodsAction->where(array('product_id' => $product_id, 'gid' => $goodsInfo->goods_id, 'order' => 'action_time asc'))); //'order' => 'gid desc'
        $temp = array();
        if (!empty($firstactionList)) {
            $data['product_id'] = $product_id;
            foreach ($firstactionList AS $key => $value) {
                $value = $value->attributes;
                $temp[$key]['action_type'] = $value['action_type'];
                if ($temp[$key]['action_type'] == 0) {
                    $temp[$key]['action_type_name'] = '商家报名';
                    $temp[$key]['note'] = '';
                    $temp[$key]['color'] = 0;
                } else if ($temp[$key]['action_type'] == 1) {
                    $temp[$key]['color'] = 0;
                    $temp[$key]['action_type_name'] = '审核通过';
                    $temp[$key]['note'] = '上架时间：' . $value['note'];
                } else if ($temp[$key]['action_type'] == 2) {
                    $temp[$key]['color'] = 1;
                    $temp[$key]['action_type_name'] = '审核不通过';
                    $temp[$key]['note'] = '不通过原因：' . $value['note'];
                } else if ($temp[$key]['action_type'] == 3) {
                    $temp[$key]['color'] = 0;
                    $temp[$key]['action_type_name'] = '删除商品';
                    $temp[$key]['note'] = '';
                } else if ($temp[$key]['action_type'] == 4) {
                    $temp[$key]['color'] = 0;
                    $temp[$key]['action_type_name'] = '管理员后台添加';
                    $temp[$key]['note'] = '';
                } else if ($temp[$key]['action_type'] == 5) {
                    $temp[$key]['color'] = 0;
                    $temp[$key]['action_type_name'] = '邮寄样品';
                    $temp[$key]['note'] = '';
                } else if ($temp[$key]['action_type'] == 6) {
                    $temp[$key]['color'] = 0;
                    $temp[$key]['action_type_name'] = '用户取消报名';
                    $temp[$key]['note'] = '';
                }
                $temp[$key]['action_time'] = $value['action_time'];
            }
        } else {
            return '';
        }
        //历史报名记录
        $goodsbmlistModel = $goodsBm->findAll($goodsBm->where(array('product_id' => $product_id, 'order' => 'goods_id asc')));
        if (empty($goodsbmlistModel)) {
            return '';
        }
        foreach ($goodsbmlistModel as $key => $value) {
            $action = $goodsAction->find($goodsAction->where(array('gid' => $value->goods_id, 'order' => 'action_time DESC')));
            //修改报名时间, 最后一个动作的结束时间
            //$key = $value->goods_id;
            $histoylist[$key]['baoming_time'] = $action->baoming_time;
            $histoylist[$key]['action_time'] = $action->action_time;
            $histoylist[$key]['isdelivery'] = $value->isdelivery;
            $histoylist[$key]['action_type'] = $action->action_type;
            $histoylist[$key]['note'] = $action->note;
            if ($histoylist[$key]['action_type'] == 0) {
                $histoylist[$key]['action_type_name'] = '商家报名';
                $histoylist[$key]['note'] = '';
            } else if ($histoylist[$key]['action_type'] == 1) {
                $histoylist[$key]['action_type_name'] = '审核通过';
                $histoylist[$key]['note'] = '上架时间：' . $action['note'];
            } else if ($histoylist[$key]['action_type'] == 2) {
                $histoylist[$key]['action_type_name'] = '审核不通过';
            } else if ($histoylist[$key]['action_type'] == 3) {
                $histoylist[$key]['action_type_name'] = '删除商品';
                $histoylist[$key]['note'] = '';
            } else if ($histoylist[$key]['action_type'] == 4) {
                $histoylist[$key]['action_type_name'] = '手动添加！';
                $histoylist[$key]['note'] = '';
            } else if ($histoylist[$key]['action_type'] == 5) {
                $histoylist[$key]['action_type_name'] = '请邮寄样品';
            } else if ($histoylist[$key]['action_type'] == 6) {
                $histoylist[$key]['action_type_name'] = '用户取消报名';
                $histoylist[$key]['note'] = '';
            }
        }
//        $cache = array($data, $temp, $histoylist);
//        Yii::app()->cache->set($cacheKey, $cache, 300);
        if (!empty($data['product_id'])) {
            $this->ajaxReturn(array('status' => true, 'detail' => $data, 'temp' => $temp, 'info' => "产品折后价过高，请降低折后价格或换其他给力产品", 'histoylist' => $histoylist));
        }
        $this->ajaxReturn(array('status' => false, 'detail' => '', 'temp' => '', 'info' => "未找到报名商品，请检查查询ID是否正确！", 'histoylist' => '', 'error_code' => 1012));
    }

    /**
     * 兑换积分商品功能
     * @return boolean
     */
    public function actionDoExch() {
        $model = RedeemModel::model();
        $id = (int) Yii::app()->request->getParam('id');
        if (empty($id)) {
            $this->ajaxReturn(array('status' => 0, 'info' => '找不到商品id', 'error_code' => 1120));
        }
        $objModel = $model->findByPk($id);
        if (!$objModel) {
            $this->ajaxReturn(array('status' => 0, 'info' => '找不到商品记录', 'error_code' => 1120));
        }
        if (is_array($arrResult = $objModel->doExchange($this->userInfo))) {
            $this->ajaxReturn(array('status' => 0, 'info' => $arrResult['error'], 'error_code' => $arrResult['error_code']));
        } else if ($arrResult == true) {
            $this->ajaxReturn(array('status' => 1, 'info' => '商品兑换成功，请购买好评后，提交订单，然后耐心等待工作人员的确认及邮寄商品', 'error_code' => 10000));
        } else {
            $this->ajaxReturn(array('status', 'info' => $objModel->getOneError() ? $objModel->getOneError() : '兑换失败!', 'error_code' => 1121));
        }
    }

    /**
     * 获取积分兑换记录列表
     */
    public function actionExList() {
        $offset = (int) $_GET['offset'];
        $limit = (int) Yii::app()->request->getParam('num');
        if ($limit <= 0) {
            $limit = 20;
        }
        $model = RedeemRecordsModel::model();
        $criteria = $model->where(array('uid' => $this->uid, 'order' => 'record_time desc'));
        $count = $model->count($criteria);
        $criteria->limit = $limit;
        $criteria->offset = $offset;
        $exList = $model->findAll($criteria);
        $data = array();
        foreach ($exList as $key => $t) {
            $data[$key] = $t->attributes;
            $obj = RedeemModel::model()->findByPk($t->redeem_id);
            $data[$key]['url'] = $obj->url;
            $data[$key]['img'] = $obj->thumb;
        }
        if (!empty($data)) {
            $this->ajaxReturn(array('status' => true, 'count' => $count, 'num' => $limit, 'data' => $data));
        } else {
            $this->ajaxReturn(array('status' => true, 'count' => '', 'limit' => '', 'data' => '', 'error_code' => 1117));
        }
    }

    /**
     * 修改订单号
     */
    public function actionOrdNumber() {
        $rid = (int) Yii::app()->request->getParam('rid');
        $order = Yii::app()->request->getParam('order_sn');
        if (empty($order) || empty($rid)) {
            $this->ajaxReturn(array('status' => false, 'info' => "订单号或记录id为空", 'error_code' => 1120));
        }
        $objModel = RedeemRecordsModel::model()->findByPk($rid);
        if (!$objModel) {
            $this->ajaxReturn(array('status' => false, 'info' => '找不到商品记录', 'error_code' => 1120));
        }
        $mixResult = $objModel->canEditOrder($this->uid);
        if (!$mixResult) {
            $this->ajaxReturn(array('status' => false, 'info' => $objModel->getOneError() ? $objModel->getOneError() : '该订单号不能修改!', 'error_code' => 1123));
        } else if (is_array($mixResult)) {
            $this->ajaxReturn(array('status' => false, 'info' => '支付宝或地址信息没有填写完整', 'error_code' => 1122));
        }
        $objModel->order_sn = $order;
        $objModel->order_submit_time = time();
        $objModel->save(true, array('order_sn', 'order_submit_time'));
        //获取卖家的信息 
        $objMember = MembersModel::model()->findByPk($objModel->seller_uid);
        if (!empty($objMember->email)) {
            $mail = Yii::createComponent('application.extensions.mailer.EMailer');
            $content = "用户：{$objModel->username},购买的积分商品：{$objModel->RedeemModel->title},已提交了订单,商品ID：{$objModel->RedeemModel->product_id},订单号：{$objModel->order_sn}，请尽快确认，超过三天将自动确认通过";
            $mail->sendEmail($objMember->email, '用户积分商品订单提交', $content);
        }
        if (!empty($objMember->phone)) {
            $moblie = $objMember->phone;
            $content = "用户：{$objModel->username},购买的积分商品：{$objModel->RedeemModel->title},已提交了订单,商品ID：{$objModel->RedeemModel->product_id},订单号：{$objModel->order_sn}，请尽快确认，超过三天将自动确认通过";
            Sns::getInstance()->send($moblie, $content);
        }
        //TODO 异步通知商家,买家更新了订单
        //发短信给商家
        $this->ajaxReturn(array('status' => true, 'info' => '修改成功,请耐心等待商户确认订单号!', 'error_code' => 10000));
    }

    /**
     * 修改地址
     */
    public function actionImpovMg() {
//        $rid = (int) Yii::app()->request->getParam('rid');
//       // $order = Yii::app()->request->getParam('order_sn');
//        if ( empty($rid)) {
//            $this->ajaxReturn(array('status' => false, 'info' => "记录id为空", 'error_code' => 1120));
//        }
//        $objModel = RedeemRecordsModel::model()->findByPk($rid);
//        if (!$objModel) {
//            $this->ajaxReturn(array('status' => false, 'info' => '找不到商品记录', 'error_code' => 1120));
//        }
        // $objModel->order_sn = $order;
        //$objModel->order_submit_time = time();
        //$objModel->save(true, array('order_sn', 'order_submit_time'));
        //$data['order_sn'] = $objModel->order_sn;
//        $objMember = MembersModel::model()->findByPk($objModel->seller_uid);
//        if (!empty($objMember->email)) {
//            $mail = Yii::createComponent('application.extensions.mailer.EMailer');
//            $content = "用户：{$objModel->username},购买的积分商品：{$objModel->RedeemModel->title},已提交了订单,商品ID：{$objModel->RedeemModel->product_id},订单号：{$objModel->order_sn}，请尽快确认，超过三天将自动确认通过";
//            $mail->sendEmail($objMember->email, '用户积分商品订单提交', $content);
//        }
//        if (!empty($objMember->phone)) {
//            $moblie = $objMember->phone;
//            $content = "用户：{$objModel->username},购买的积分商品：{$objModel->RedeemModel->title},已提交了订单,商品ID：{$objModel->RedeemModel->product_id},订单号：{$objModel->order_sn}，请尽快确认，超过三天将自动确认通过";
//            Sns::getInstance()->send($moblie, $content);
//        }//修改订单号
//        $MembersTaobao = MembersTaobaoModel::model()->getMemberTB($this->uid);
//        $data['wangw'] = $MembersTaobao->taobao_user_nick;
//        if (empty($data['wangw'])) {
//            $MembersTaobaoModel = new MembersTaobaoModel();
//            $wangw = Yii::app()->request->getParam('wangw');
//            if (empty($wangw)) {
//                $this->ajaxReturn(array('status' => 0, 'info' => '旺旺号不能为空!'));
//            }
//            $MembersTaobaoModel->taobao_user_nick = Yii::app()->request->getParam('wangw');
//            $MembersTaobaoModel->uid = $this->uid;
//            $MembersTaobaoModel->add_time = time();
//            if ($MembersTaobaoModel->validate()) {
//                $MembersTaobaoModel->save();
//            } else {
//                $this->ajaxReturn(array('status' => false, 'info' => $MembersTaobaoModel->getOneError() ? $MembersTaobaoModel->getOneError() : '写入失败'));
//            }
//        }
        $MembersAddress = MembersAddressModel::model()->getMemberAddres($this->uid);
        if (empty($MembersAddress->uid)) {
            $MembersAddress = new MembersAddressModel();
        }
        $data['consignee'] = Yii::app()->request->getParam('consignee');
        if (empty($data['consignee'])) {
            $this->ajaxReturn(array('status' => 0, 'info' => '联系人不为空!'));
        }
        $data['address'] = Yii::app()->request->getParam('address');
        if (empty($data['address'])) {
            $this->ajaxReturn(array('status' => 0, 'info' => '详细地址不为空!'));
        }
        $data['mobile'] = Yii::app()->request->getParam('mobile');
        if (empty($data['mobile'])) {
            $this->ajaxReturn(array('status' => 0, 'info' => '手机号不为空!'));
        }
        $data['zipcode'] = Yii::app()->request->getParam('zipcode');
        if (empty($data['zipcode'])) {
            $this->ajaxReturn(array('status' => 0, 'info' => '邮编不为空!'));
        }
        $data['uid'] = $this->uid;
        $MembersAddress->attributes = array(
            'consignee' => $data['consignee'],
            'address' => $data['address'],
            'mobile' => $data['mobile'],
            'zipcode' => $data['zipcode'],
            'uid' => $data['uid'],
        );
        if ($MembersAddress->validate()) {
            $MembersAddress->save();
        } else {
            $this->ajaxReturn(array('status' => false, 'info' => $MembersAddress->getOneError() ? $MembersAddress->getOneError() : '写入失败'));
        }

//        $userInfoObj = MembersModel::model()->findByPk($this->uid);
//        $data['alipay'] = $userInfoObj->alipay;
//        $data['alipay_name'] = $userInfoObj->alipay_name;
//        if (empty($data['alipay']) || empty($data['alipay_name'])) {
//            $ap = Yii::app()->request->getParam('alipay');
//            $apname = Yii::app()->request->getParam('alipay_name');
//            if (empty($ap)) {
//                $this->ajaxReturn(array('status' => 0, 'info' => '支付宝账号不为空'));
//            }
//            if (empty($apname)) {
//                $this->ajaxReturn(array('status' => 0, 'info' => '支付宝名字不为空'));
//            }
//            $userInfoObj->alipay = $ap;
//            $userInfoObj->alipay_name = $apname;
//            if ($userInfoObj->validate()) {
//                $userInfoObj->save();
//            } else {
//                $this->ajaxReturn(array('status' => false, 'info' => $this->getOneError() ? $this->getOneError() : '写入失败'));
//            }
//        }
        $this->ajaxReturn(array('status' => 1, 'data' => $data, 'info' => '修改成功!'));
    }

    public function actionGetMg() {
//        $key = "getMg_UserController_201409231058_{$this->uid}_";
//        $data = Yii::app()->cache->get($key);
        //  if (empty($data)) {          
        $MembersTaobao = MembersTaobaoModel::model()->getMemberTB($this->uid);
        $data['wangw'] = $MembersTaobao->taobao_user_nick;
        //旺旺号
        $MembersAddress = MembersAddressModel::model()->getMemberAddres($this->uid);
        $data['consignee'] = $MembersAddress->consignee;
        $data['address'] = $MembersAddress->address;
        $data['mobile'] = $MembersAddress->mobile;
        $data['zipcode'] = $MembersAddress->zipcode;
        //地址信息
        $userInfoObj = MembersModel::model()->findByPk($this->uid);
        $data['alipay'] = $userInfoObj->alipay;
        $data['alipay_name'] = $userInfoObj->alipay_name;
        //支付宝账号
        // Yii::app()->cache->set($key, $data, 300);
        //  }

        $this->ajaxReturn(array('status' => 1, 'data' => $data, 'info' => '获取成功!'));
    }

    //晒单
    public function actionSd() {
        $exprience = Yii::app()->request->getParam('exprience');
        $ordersn = Yii::app()->request->getParam('ordersn');
        Yii::import('ext.yzwAPI.*');
        if (!DzAPI::getInstance()->canSunsingle()) {
            $this->ajaxReturn(array('status' => 0, 'info' => "请先到论坛激活账号再晒单!", 'error_code' => 1126));
        }
        $objModel = new MembersSunsingleModel('create');
        $objModel->attributes = array(
            'ordersn' => $ordersn,
            'uid' => $this->uid,
            'experience' => $exprience,
        );
        if ($objModel->save()) {
            $objSalesModel = SalesDataModel::model()->find(array('condition' => 'orders_number = :orders_number', 'params' => array('orders_number' => $objModel->ordersn)));
            if (!$objSalesModel) {//在销售数据里面能找到此订单记录
                $this->ajaxReturn(array('status' => 1, 'info' => "提交成功,三天内给出处理结果,请耐心等候系统通知"));
            }
            if ($objModel->post($objSalesModel)) {
                $objModel->status = 1; //处理成功
                $objModel->save(false, array('status'));
                $this->ajaxReturn(array('status' => 1, 'info' => "晒单成功"));
            } else {
                $this->ajaxReturn(array('status' => 1, 'info' => "提交成功,三天内给出处理结果,请耐心等候系统通知"));
            }
        } else {
            $this->ajaxReturn(array('status' => 0, 'info' => $objModel->getOneError() ? $objModel->getOneError() : "提交失败!", 'error_code' => 1127));
        }
    }

    //积分晒单
    public function actionJfsd() {
        $id = Yii::app()->request->getParam('id');
        $experience = Yii::app()->request->getParam('experience');
        yii::import('ext.yzwAPI.*');
        if (!DzAPI::getInstance()->canSunsingle()) {
            $this->ajaxReturn(array('status' => 0, 'info' => "请先到论坛激活账号再晒单!", 'error_code' => 1126));
        }
        if (!$experience) {
            $this->ajaxReturn(array('status' => 0, 'info' => "心得内容不能为空!", 'error_code' => 1128));
        }
        $objModel = RedeemRecordsModel::model()->findByPk($id);
        $uid = yii::app()->user->id;
        if ($objModel->is_experience > 0) {
            $this->ajaxReturn(array('status' => 0, 'info' => "此积分记录已经晒过单!", 'error_code' => 1129));
        }
        if (!$objModel || $objModel->uid != $uid) {
            $this->ajaxReturn(array('status' => 0, 'info' => "找不到记录!", 'error_code' => 1130));
        }
        if ($objModel->post($experience)) {
            $this->ajaxReturn(array('status' => 1, 'info' => "晒单成功!"));
        } else {
            $this->ajaxReturn(array('status' => 0, 'info' => "晒单失败!", 'error_code' => 1127));
        }
    }

    //晒单历史记录
    public function actionSdlist() {
        $model = new MembersSunsingleModel();
        $offset = (int) $_GET['offset'];
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 10;
        }
        $criteria = $model->where(array('uid' => $this->uid, 'order' => 'update_time desc'));
        $sdlist = $model->findAll($criteria);
        $criteria->limit = $num;
        $criteria->offset = $offset;
        if (!empty($sdlist)) {
            $data = array();
            foreach ($sdlist as $p => $sd) {
                $data[$p] = $sd->attributes;
            }
            $this->ajaxReturn(array('status' => 1, 'info' => '读取晒单记录成功', 'detail' => $data));
        } else {
            $this->ajaxReturn(array('status' => 0, 'info' => '读取晒单记录失败', 'detail' => ''));
        }
    }

    //手机分享增加积分
    public function actionShare() {
        $key = "App_UserController_share_20141030_" . $this->uid;
        $cache = Yii::app()->cache->get($key);
        $time = time();
        $today = strtotime("today");
        if (empty($cache)) {
            Yii::app()->cache->set($key, $time, $today + 86400 - $time);
        } else {
            if ($cache > $today) {
                $this->ajaxReturn(array('status' => 0, 'info' => "您今天已经分享过!", 'error_code' => 1131));
            } else {
                Yii::app()->cache->set($key, $time, $today + 86400 - $time);
            }
        }
        //增加积分
        if (MembersModel::model()->plusScore($this->uid, 10) == true) {
            MembersActionModel::model()->addShareAction($this->uid, '手机分享送积分活动，赠送积分 10 。');
            $this->ajaxReturn(array('status' => 1, 'info' => "分享增加积分成功,感谢您的支持!"));
        } else {
            $this->ajaxReturn(array('status' => 0, 'info' => "分享加积分失败,请重新分享!", 'error_code' => 1132));
        }
    }

}
